﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Order_Default : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {


        if (Session["Username"] != null)
        {
            btnRedirect.Visible = true;
            BtnLogin.Visible = false;
            BtnLogout.Visible = true;
            BtnOrder.Visible = true;
        }
        else
        {
            BtnOrder.Visible = false;
            btnRedirect.Visible = false;
            BtnLogin.Visible = true;
            BtnLogout.Visible = false;
        }


        if (!IsPostBack)
        {
            if (Session["Username"] == null)
            {
                Response.Redirect("../Login/Login.aspx");
            }
        }
        string s = ((clsLogIn)Session["Username"]).UserName;
        String strCon = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString();
        SqlConnection myCon = new SqlConnection(strCon);
        myCon.Open();
        string sql = "select UID from _User where Username = '{0}'";
        sql = string.Format(sql,s);
        SqlCommand cmd = new SqlCommand(sql, myCon);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        da.Fill(ds);
        Session["UID"] = ds.Rows[0][0].ToString();
        showOrder();
    }
    private void showOrder() 
    {
        String strCon = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString();
        SqlConnection myCon = new SqlConnection(strCon);
        myCon.Open();
        
        SqlCommand cmd = new SqlCommand("select * from _Order", myCon);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        da.Fill(ds);
        tbDetailOrder.DataSource = ds;
        tbDetailOrder.DataBind();
        for (int i = 0; i < tbDetailOrder.Rows.Count; i++) 
        {
            string sql = "select Price from _Product where PID = '{0}'";
            sql = string.Format(sql,tbDetailOrder.Rows[i].Cells[3].Text);
            SqlCommand cmd1 = new SqlCommand(sql,myCon);
            SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
            DataTable ds1 = new DataTable();
            da1.Fill(ds1);
            tbDetailOrder.Rows[i].Cells[1].Text = "" + int.Parse(tbDetailOrder.Rows[i].Cells[5].Text)*float.Parse(ds1.Rows[0][0].ToString());
        }
        
        myCon.Close();
    }
    protected void tbDetailOrder_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

        String strCon = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString();
        SqlConnection myCon = new SqlConnection(strCon);
        myCon.Open();
        string sql2 = "select * from _Customer where OID = {0}";
        sql2 = string.Format(sql2,int.Parse(tbDetailOrder.Rows[e.NewSelectedIndex].Cells[2].Text));
        SqlCommand cmd2 = new SqlCommand(sql2, myCon);
        SqlDataAdapter da = new SqlDataAdapter(cmd2);
        DataTable dt = new DataTable();
        da.Fill(dt);
        lblOID.Text = dt.Rows[0][0].ToString();
        lblName.Text = dt.Rows[0][1].ToString();
        lblAddress.Text = dt.Rows[0][2].ToString();
        lblPhone.Text = dt.Rows[0][3].ToString();
        string sql1 = "update _Order set UID = '{0}' where OID = {1}";
        sql1 = string.Format(sql1, Session["UID"], int.Parse(tbDetailOrder.Rows[e.NewSelectedIndex].Cells[2].Text));
        SqlCommand cmd1 = new SqlCommand(sql1, myCon);
        try
        {
            cmd1.ExecuteNonQuery();
        }
        catch (Exception)
        {
        }
        string sql = null;
        CheckBox ck = (CheckBox)tbDetailOrder.Rows[e.NewSelectedIndex].Cells[6].Controls[0];

        if (!ck.Checked)
        {
            sql = "update _order set Status = 1 where OID = '{0}'";
        }
        else 
        {
            sql = "update _order set Status = 0,UID = null where OID = '{0}'";
        }
        sql = string.Format(sql,tbDetailOrder.Rows[e.NewSelectedIndex].Cells[2].Text);
        SqlCommand cmd = new SqlCommand(sql, myCon);
        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
        }
        showOrder();
    }



    protected void btnRedirect_Click(object sender, EventArgs e)
    {
        clsLogIn a = (clsLogIn)(Session["Username"]);

        if (a.IsAdmin)
        {
            Response.Redirect("../User Information/Information.aspx");
        }
        else
        {
            Response.Redirect("../Add Products/AddProducts.aspx");
        }
    }
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Login/Login.aspx");

    }
    protected void BtnLogout_Click1(object sender, EventArgs e)
    {
        Session["Username"] = null;
        Response.Redirect("../Login/Login.aspx");
    }
    protected void BtnOrder_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Order/Default.aspx");
    }
}